package com.ai.demo.controller;

import com.ai.demo.enums.ChatTypeEnum;
import com.ai.demo.service.ISpringAiChatRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * 聊天控制器
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {

    private final ChatClient  chatClient;

    private final ISpringAiChatRecordService springAiChatRecordService;

    @RequestMapping(value = "/chat",produces = "text/html;charset=utf-8")
    public Flux<String> chat(@RequestParam String prompt,String chatId) {
        System.out.println("prompt: " + prompt);
        springAiChatRecordService.saveRecord(ChatTypeEnum.CHAT.getCode(),chatId);
        return chatClient.prompt()
                .user(prompt)
                .advisors(obj->obj.param(ChatMemory.CONVERSATION_ID,chatId))
                .stream()
                .content();
    }

}
